package com.jc.order.dto;


import com.jc.order.util.SQLFilter;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 查询参数
 */
public class Query implements Serializable {

    private static final long serialVersionUID = -1L;
    // 当前页码
    private int pageNum;
    // 每页条数
    private int pageSize;

    /**
     * 排序方式 desc asc
     */
    private String order;

    /**
     * 排序字段
     */
    private String sidx;

//	/**
//	 *  权限数据 用户ids
//	 */
//	private List<Long> limitDataIds;


//    private List<Long> userLimit;

    private LinkedHashMap<String, Object> m;

    /**
     * @return the m
     */
    public LinkedHashMap<String, Object> getM() {
        return m;
    }

    /**
     * @param m the m to set
     */
    public void setM(LinkedHashMap<String, Object> m) {
        this.m = m;
    }

    public static final String PAGE_NUM_KEY = "pageNum";
    public static final String PAGE_SIZE_KEY = "pageSize";
    public static final String ORDER_KEY = "order";
    public static final String SIDX_KEY = "sidx";

    public static final Integer DEFAULT_PAGE_NUM = 1;
    public static final Integer DEFAULT_PAGE_SIZE = 15;

    public Query() {
        this.m = new LinkedHashMap<String, Object>();
    }

    public Query(Map<String, Object> params) {
        this.m = new LinkedHashMap<String, Object>();

        this.putAll(params);

        params.putIfAbsent(PAGE_NUM_KEY, DEFAULT_PAGE_NUM);
        params.putIfAbsent(PAGE_SIZE_KEY, DEFAULT_PAGE_SIZE);

        // 分页参数
        this.setPageNum(Integer.parseInt(params.get(PAGE_NUM_KEY).toString()));
        this.setPageSize(Integer.parseInt(params.get(PAGE_SIZE_KEY).toString()));
        this.put(PAGE_NUM_KEY, pageNum);
        this.put(PAGE_SIZE_KEY, pageSize);

        // 防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        if (params.get(SIDX_KEY) != null) {
            this.setSidx(params.get(SIDX_KEY).toString());
            this.put(SIDX_KEY, SQLFilter.sqlInject(sidx));
        }
        if (params.get(ORDER_KEY) != null) {
            this.setOrder(params.get(ORDER_KEY).toString());
            this.put(ORDER_KEY, SQLFilter.sqlInject(order));
        }

    }

    public void put(String key, Object value) {
        m.put(key, value);
    }

    public void putAll(Map<String, Object> m) {
        this.m.putAll(m);
    }

    public int getPageNum() {
        return pageNum;
    }

    public Object get(String key) {
        return m.get(key);
    }

    public void setPageNum(int pageNum) {
        this.put(PAGE_NUM_KEY, pageNum);
        this.pageNum = pageNum;
    }

    /**
     * @return the pageSize
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(int pageSize) {
        this.put(PAGE_SIZE_KEY, pageSize);
        this.pageSize = pageSize;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getSidx() {
        return sidx;
    }

    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

//	public List<Long> getLimitDataIds() {
//		return limitDataIds;
//	}
//
//	public void setLimitDataIds(List<Long> limitDataIds) {
//		this.getM().put("limitUserIds", StringUtils.join(limitDataIds, "','"));
//		this.limitDataIds = limitDataIds;
//	}
//
//    public List<Long> getUserLimit() {
//        return userLimit;
//    }
//
//    public void setUserLimit(List<Long> userLimit) {
//        this.userLimit = userLimit;
//    }
}
